<!DOCTYPE html>
<html lang="zh-CN" style="overflow: hidden;">

<!-- index 启动页面 -->

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="./js/vue.min.js"></script>
    <script src="./js/element-ui/index.js"></script>
    <link rel="stylesheet" href="./js/element-ui/index.css">
    <script src="./config.js"></script> 

    <title>斗轮机无人值守系统-南环科技</title>
</head>

<body style="margin: 0px">
    <div id="app">
        <!-- <iframe id="mainIframe" align="center" width="100%" height="1080px" frameborder="no" border="0" marginwidth="0"
            marginheight="0" scrolling="no">
        </iframe> -->
        <el-dialog title="登录" :visible.sync="centerDialogVisible" width="30%" center :close-on-click-modal="false"
            :show-close="false" :close-on-press-escape="false">
            <el-row>
                <el-col :span="24">
                    <el-input v-model="userName" placeholder="请输入用户名"></el-input>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-input v-model="password" placeholder="请输入密码" show-password></el-input>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="loginOperator()">操作员登录</el-button>
                <el-button type="primary" @click="confirmPassword()">确 定</el-button>
            </span>
        </el-dialog>
    </div>

    <script>
        let autologin = false;
        let vueMain = null;
        const Main = {
            data() {
                return {
                    centerDialogVisible: true,
                    password: '',
                    userName: ''
                }
            },

            methods: {
                confirmPassword() {
                    if (this.password === '' || this.userName === '') {
                        this.$message.error('请输入用户名密码！');
                        return;
                    }
                    this.login(this.userName, this.password);
                },
                login(userName, password) {
                    let request = {};
                    request.Function = "LogIn";
                    request.UserName = userName;
                    request.Password = password;
                    this.sendPostMessage(g_CenterHttpUrl, request);
                },
                sendPostMessage(serverUrl, request) {
                    if (serverUrl == null) {
                        return;
                    }
                    if (request == null) {
                        return;
                    }
                    fetch(serverUrl, {
                        method: 'post',
                        body: JSON.stringify(request),
                        headers: { 'Content-Type': 'application/json' }
                    }).then((e) => {
                        e.text().then(data => {
                        let jdata = JSON.parse(data);
                            if(jdata.Code == g_ResCodeSuccess) {
                                let userInfo = jdata["Message"];
                                window.open("http://localhost:8001/wrzs_nd/","_self");
                                // window.meta2d.setValue({ id: 'iframeUrl', iframe: "/2d?id=wrzs" });
                                // document.getElementById('mainIframe').src = "./home.html?userName=" + escape(userInfo.REAL_NAME) + "&userRights=" + userInfo.USER_PRIVILEGE;
                                vueMain.centerDialogVisible = false;
                                localStorage.setItem("userRights",userInfo.Permission);
                            }
                            else {
                                vueMain.$message.warning(jdata.Message);
                            }
                        });
                    });
                },
                loginOperator() {
                    this.userName = commonUser;
                    this.password = commonUserPwd;
                    this.confirmPassword();
                }
            },

            mounted() {
                vueMain = this;
                if (autologin){
                    this.userName = commonUser;
                    this.password = commonUserPwd;
                    this.confirmPassword();
                }
            }
        };

        function getUser(){
            let request = {};
            request.Function = "GetCurrentUser";
            fetch(g_CenterHttpUrl, {
                method: 'post',
                body: JSON.stringify(request),
                headers: { 'Content-Type': 'application/json' }
            }).then((e) => {
                e.text().then(data => {
                let jdata = JSON.parse(data);
                    if(jdata.Code == g_ResCodeSuccess) {
                        autologin = true;
                    }
                    else {
                        vueMain.$message.warning(jdata.Message);
                    }
                    mountVue();
                });
            });
        }

        function mountVue(){
            let Component = Vue.extend(Main);
            new Component().$mount('#app');
        }

        window.onload = function () {
            // getUser();
            mountVue();
        };

        window.addEventListener('message', function (event) {
            try {
                let arrMessage = event.data.split("_");
                if (arrMessage[0] == "signout") {
                    location.reload();
                }
            }
            catch (ex) {
                console.log("ex: " + ex);
            }
        });
    </script>
</body>

</html>
